package com.rgs.module.file.service.impl;

import com.rgs.base.database.SqlPlusUtil;
import com.rgs.module.file.entity.FileInfoDO;
import com.rgs.module.file.filter.FileInfoFilterDTO;
import com.rgs.module.file.mapper.FileInfoMapper;
import com.rgs.module.file.service.FileInfoService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author Ren Gaoshuai
 * @ClassName FileInfoServiceImpl
 * @date 2022-01-26 8:57
 **/
@Service
public class FileInfoServiceImpl implements FileInfoService {
    @Resource
    FileInfoMapper fileInfoMapper;

    @Override
    public Optional<FileInfoDO> selectByFilter(FileInfoFilterDTO fileInfoFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(FileInfoDO.class, fileInfoFilter);
        return fileInfoMapper.selectByExample(example).stream().findFirst();
    }

    @Override
    public List<FileInfoDO> listByFilter(FileInfoFilterDTO fileInfoFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(FileInfoDO.class, fileInfoFilter);
        return fileInfoMapper.selectByExample(example);
    }

    @Override
    public void updateSelectiveByFilter(FileInfoDO fileInfoDO, FileInfoFilterDTO fileInfoFilter) {
        Example example = SqlPlusUtil.sqlFilterJoin(FileInfoDO.class, fileInfoFilter);
        fileInfoMapper.updateByExampleSelective(fileInfoDO, example);
    }

    @Override
    public FileInfoDO selectById(String id) {
        return fileInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public FileInfoDO insert(FileInfoDO fileInfo) {
        fileInfoMapper.insert(fileInfo);
        return fileInfo;
    }
}
